package com.agents.builder.common.ai.strategy.impl;

import com.agents.builder.common.ai.enums.LlmProvider;
import com.agents.builder.common.ai.strategy.common.BaseChatModelBuilder;
import com.agents.builder.common.core.utils.SpringUtils;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.model.function.DefaultFunctionCallbackResolver;
import org.springframework.ai.model.function.FunctionCallbackResolver;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OpenaiChatModelBuilder extends BaseChatModelBuilder {


    @Override
    public LlmProvider provider() {
        return LlmProvider.OPEN_AI;
    }


    @Override
    public ChatModel getChatModel(String apiKey, String baseUrl,String modelName) {
        DefaultFunctionCallbackResolver functionCallbackResolver = new DefaultFunctionCallbackResolver();
        functionCallbackResolver.setApplicationContext(SpringUtils.getApplicationContext());
        return new OpenAiChatModel(new OpenAiApi(baseUrl, apiKey), OpenAiChatOptions.builder().model(modelName).build(),functionCallbackResolver, RetryUtils.DEFAULT_RETRY_TEMPLATE);
    }
}
